//
//  RegistViewController.m
//  TribleWar
//
//  Created by ＣＳＴ ＣＳＴ on 8/10/12.
//  Copyright (c) 2012 ＣＳＴ. All rights reserved.
//

#import "RegistViewController.h"
#import "GameUser.pb.h"
#import "NetWorkHandler.h"
#import "AppDelegate.h"


@implementation RegistViewController
@synthesize pwd = _pwd;
@synthesize account =_account;
@synthesize repwd = _repwd;
@synthesize registBtn = _registBtn;
@synthesize loginBtn = _loginBtn;
@synthesize status = _status;
@synthesize connectStatus;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)registCallback:(Ack *)ack
{
    connectStatus = @"服务器异常，注册失败";
    if (!ack) {
        [_status setText:connectStatus];
        NSLog(@"ack is nil");
        return;
    }
    if ([ack success]== NO ) {
        NSLog(@"ssss");
        if ([[ack msg] isEqualToString:@""]) {
            NSLog(@"ack with a empty msg");

            [_status setText:connectStatus];
            connectStatus = @"服务器异常，注册失败";
            return;
        }
        [_status setText:[ack msg]];
        connectStatus = [ack msg];
    }
    else{
        connectStatus = @"注册成功";
        [_status setText:connectStatus];
    }
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewWillAppear:(BOOL)animated
{
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"cracked_earth_texture.png"]];
    self.view.backgroundColor = background;
    _status.lineBreakMode = UILineBreakModeWordWrap;
    _status.numberOfLines = 0;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//hide keyboard after editing
- (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
    for (UIView* view in self.view.subviews) 
    {
        if ([view isKindOfClass:[UITextField class]])
            [view resignFirstResponder];
    }
}

- (IBAction)onRegist
{
    NSString *password = [_pwd text];
    NSString *repassword = [_repwd text];
    NSString *account = [_account text];
    if (![self isValid:account password:password repassword:repassword]) {
        NSLog(@"非法注册信息");
        return; 
    }
    User *user = [[[[[User builder]setAction:ActionCreate]setAccount:account]setPassword:password]build];
    NSData *userData = [user data];
    
    [[AppDelegate sharedAppDelegate]->netWorkHandler_ sendData:userData Pid:PIDUser];
}


- (IBAction)onLogin:(id)sender;
{
    [self.view removeFromSuperview];  
}

- (BOOL)isValid:(NSString *)account password:(NSString *)pwd repassword:(NSString *)repwd
{
    NSString * regex = @"(^[A-Za-z0-9]{3,10}$)";  
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
    if (![pred evaluateWithObject:account]) {
        connectStatus = @"帐号必须大于3位且只能含有数字与字母";
        _status.text = connectStatus;
        return NO;
    }
    
    if ([pwd length]<6||[repwd length]<6) {
        connectStatus = @"密码必须大于6位";
        _status.text = connectStatus;
        return NO;
    }
    
    if (![pwd isEqualToString:repwd])
    {
        connectStatus = @"两次密码输入不一致";
        [_status setText:connectStatus];
        return NO;
    }    
    return YES;
}

@end
